﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using System.Text.RegularExpressions;

namespace LevelEditor
{
    public partial class NewObject : Form
    {
        private Image ObjectImage;
        private String ObjectImageURL;
        private LevelEditor editor;
        private String relURL;
        public NewObject(LevelEditor editor)
        {
            this.editor = editor;
            InitializeComponent();
        }

        private void browseImageButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "PNG Files (*.png)|*.png| Image Files (*.bmp)|*.bmp|JPEG Files (*.jpeg)|*.jpeg|" + " All Files (*.*)|*.*";
            openFile.ShowDialog();
            ObjectImage = new Bitmap(openFile.FileName);
            customImageBox.BackgroundImage = ObjectImage;
            ObjectImageURL = openFile.FileName;
            //this is done to make the relative URL
            int strlen = Environment.CurrentDirectory.Length-1;
            relURL = ObjectImageURL.Substring(strlen,((ObjectImageURL.Length)-strlen));
            objectImageUrl.Text = relURL;
            customImageBox.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CustomObject cuObject = new CustomObject(ObjectImage,
                (String)bodyTypeBox.SelectedItem,
                new Microsoft.Xna.Framework.Vector2(0, 0),
                (float)Convert.ToDouble(objectRotationBox.Text),
                objectnameTextBox.Text,
                (float)Convert.ToDouble(objectMassBox.Text),
                new Vector2(ObjectImage.Width, ObjectImage.Height),
                ObjectImageURL);
            cuObject.relativeImageUrl = relURL;
            switch (typeBox.Text)
            {
                case "Normal": editor.normalBox.Items.Add(cuObject.name);
                    cuObject.currentType = CustomObject.OType.Normal; break;
                case "Ground": editor.groundBox.Items.Add(cuObject.name);
                    cuObject.currentType = CustomObject.OType.Ground; break;
                case "Item": editor.itemBox.Items.Add(cuObject.name);
                    cuObject.currentType = CustomObject.OType.Item; break;
            }
            editor.objects.Add(cuObject);
            editor.objectsOnScreen.Add(cuObject);
            editor.pictureBox1.Update();
            editor.pictureBox1.Invalidate();
            editor.texthandler.save();
            this.Dispose();
        }

        private void NewObject_MouseClick(object sender, MouseEventArgs e)
        {
            Console.WriteLine("new");
        }
    }
}
